package com.ygxsk.carhome;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.work.Data;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

import com.blankj.utilcode.util.LogUtils;
import com.tencent.smtt.export.external.TbsCoreSettings;
import com.tencent.smtt.sdk.QbSdk;
import com.tencent.smtt.sdk.TbsListener;
import com.ygxsk.carhome.constant.ConstantSign;
import com.ygxsk.integrated.retrofit.Parameterkey;

import java.util.HashMap;

public class X5CorePreLoadWorkManager extends Worker {

    private Context mContext;
    /**
     * 参数内容
     */
    private WorkerParameters mWorkerParams;


    public X5CorePreLoadWorkManager(@NonNull Context context, @NonNull WorkerParameters _workerParams) {
        super(context, _workerParams);
        mContext = context;
        mWorkerParams = _workerParams;
    }


    /**
     * 设置回调 设置X5初始化完成的回调接口
     */
    QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {

        @Override
        public void onViewInitFinished(boolean arg0) {
            // //x5內核初始化完成的回调，为true表示x5内核加载成功，否则表示x5内核加载失败，会自动切换到系统内核。
            //LogUtils.d("itchen--X5CorePreLoadService--onViewInitFinished-->"+arg0);
            //new WebView(X5CorePreLoadService.this);
        }

        @Override
        public void onCoreInitFinished() {
            // retain
            //LogUtils.d("itchen---X5CorePreLoadService---onCoreInitFinished");
            //QbSdk.initX5Environment(getApplicationContext(), null);
        }
    };


    @NonNull
    @Override
    public Result doWork() {

        //任务的具体行为
//        LogUtils.d(ConstantSign.LOGCAT_NOMAL, "Work doWork");

        if (mWorkerParams != null) {

            final String dataString = mWorkerParams.getInputData().getString(Parameterkey.key);

            if (!TextUtils.isEmpty(dataString)) {

                initX5();

            }

            //反馈数据给发送方：
            //反馈数据给发送方；
            Data outPutData = new Data.Builder().putString(Parameterkey.doworkkey, Parameterkey.doworkok).build();
            @SuppressLint("RestrictedApi") Result.Success success = new Result.Success(outPutData);

            return success;
        }

        return Result.failure();
        //return Result.success();
    }


    private void initX5() {

        HashMap map = new HashMap();
        map.put(TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER, true);
        map.put(TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE, true);
        QbSdk.initTbsSettings(map);

        //QbSdk.setNeedInitX5FirstTime(true);//20190815升级之后不可用
        QbSdk.setDownloadWithoutWifi(true);
        QbSdk.canLoadX5FirstTimeThirdApp(this.getApplicationContext());

        QbSdk.initX5Environment(getApplicationContext(), cb);

        //LogUtils.d("itchen--初始化x5环境--initX5Environment");
        //=========================================================================

        //预加载=========================================================================
        if (!QbSdk.isTbsCoreInited()) {
            //LogUtils.d("itchen--未初始化QbSdk---开始执行重新初始化");
            QbSdk.preInit(getApplicationContext(), cb);
            // 设置X5初始化完成的回调接口
            //X5webview的preinit接口只是降低了webview的冷启动时间
            //preinit只需要调用一次，如果已经完成了初始化，那么就直接构造view

        } else {
            //LogUtils.d("itchen--QbSdk已经初始化成功");
        }
        //=========================================================================
        // 搜集本地tbs内核信息并上报服务器，服务器返回结果决定使用哪个内核。
        //TbsDownloader.needDownload(getApplicationContext(),false);
        //TbsDownloader.needDownload()

        QbSdk.setTbsListener(new TbsListener() {

            @Override
            public void onDownloadFinish(int i) {

                LogUtils.d(ConstantSign.LOGCAT_NOMAL, "itchen---onDownloadFinish--->" + i);
//                IntentService[itchen], com.ygxsk.carhome.X5CorePreLoadWorkManager$2.onDownloadFinish(X5CorePreLoadWorkManager.java:94)
//                args[0] = itchen-->
//                args[1] = itchen---onDownloadFinish--->-122
            }

            @Override
            public void onInstallFinish(int i) {
                LogUtils.d(ConstantSign.LOGCAT_NOMAL, "itchen---onInstallFinish--->" + i);
                //TbsHandlerThread, com.dgj.dinggovern.X5CorePreLoadService$1.onInstallFinish(X5CorePreLoadService.java:79)
                //itchen---onInstallFinish--->200
            }

            @Override
            public void onDownloadProgress(int i) {
//                LogUtils.d(ConstantSign.LOGCAT_NOMAL, "itchen---onDownloadProgress--->" + i);
            }
        });
    }

}
